import {Component, Inject, Optional} from '@angular/core';
import {MAT_DIALOG_DATA} from "@angular/material";
import {GnForm, GnMessageService} from "geminis-ng";
import {HttpClient} from "@angular/common/http";
import {GnFormConfig} from "../../../api/FormConfig";

@Component({
  selector: 'gn-workflow-user-work-start-form',
  templateUrl: './startForm.component.html'
})
export class StartFormComponent extends GnForm {

  formConfig: GnFormConfig;

  constructor(@Inject(MAT_DIALOG_DATA) private param: string,
              private httpClient: HttpClient,
              private messageService: GnMessageService) {
    super();

    this.httpClient.get("/WORKFLOW-SERVICE/forms/process/" + this.param)
      .subscribe((data: GnFormConfig) => {
          this.formConfig = data;
        },
        (error) => {
          const submitError = {
            message: "获取流程开始表单错误",
            error: error
          };
          this.messageService.error(submitError);
        });
  }

  complete(): void {
    this.httpClient.post("/WORKFLOW-SERVICE/processes/" + this.param + "/start", this.formComponent.form.fullValue, {responseType: 'text'})
      .subscribe(() => {
          this.dialog.confirmForm();
          this.messageService.success('发起任务成功');
        },
        (error) => {
          const submitError = {
            message: "处理错误",
            error: error
          };
          this.messageService.error(submitError);
        });
  }

}
